<?php

namespace App\Admin\Controllers;

use App\Models\Goods;
use App\Models\RecordStorage;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Arr;

class RecordStorageController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '存取酒记录';
    protected $type = [1 => '存酒', 2 => '取酒'];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new RecordStorage());

        $grid->column('store_id', __('Store id'))->hide();
        $grid->column('name', __('Name'));
        $grid->column('mobile', __('Mobile'));
        $grid->column('goods.name', '商品名称');
        $grid->column('number', '数量');
        $grid->column('type', '类型')->using($this->type)->dot([1 => 'success', 2 => 'danger']);
        $grid->column('created_at', __('Created at'))->sortable();

        $grid->filter(function ($filter) {
            $filter->disableIdFilter();

            $filter->like('name', __('Name'));
            $filter->equal('mobile', __('Mobile'));
            $filter->equal('type', '类型')
                ->radio(Arr::add($this->type, '', '全部'));
        });
        $grid->disableCreateButton();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(RecordStorage::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('store_id', __('Store id'));
        $show->field('name', __('Name'));
        $show->field('mobile', __('Mobile'));
        $show->field('goods_id', __('Goods id'));
        $show->field('number', __('Number'));
        $show->field('type', __('Type'));
        $show->field('description', __('Description'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('deleted_at', __('Deleted at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new RecordStorage());

        $form->hidden('store_id', __('Store id'));
        $form->text('name', __('Name'))->disable();
        $form->mobile('mobile', __('Mobile'))->rules("required|regex:/^1[34578]{1}\d{9}$/");
        $form->select('goods_id', '商品')->options(Goods::all()->pluck('name', 'id'));
        $form->number('number', '数量')->required();
        $form->radio('type', __('Type'))->options($this->type)->default(1);
        $form->text('description', __('Description'))->rules("required");

        $form->footer(function (Form\Footer $footer) {
            $footer->disableReset();
            $footer->disableCreatingCheck();
        });
        return $form;
    }
}
